{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### reuse有什么用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "class MyModel(tf.keras.Model):\n",
    "    def __init__(self):\n",
    "        super(MyModel, self).__init__()\n",
    "        self.dense = tf.layers.Dense(10)\n",
    "    \n",
    "    def call(self, inputs):\n",
    "        with tf.variable_scope(\"my_model\", reuse=tf.AUTO_REUSE):\n",
    "            output = self.dense(inputs)\n",
    "        return output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = MyModel()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "input1 = tf.random.normal((32, 5))\n",
    "output1 = model(input1)\n",
    "\n",
    "\n",
    "input2 = tf.random.normal((16, 5))\n",
    "output2 = model(input2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<tf.Variable 'my_model/dense/kernel:0' shape=(5, 10) dtype=float32>, <tf.Variable 'my_model/dense/bias:0' shape=(10,) dtype=float32>]\n"
     ]
    }
   ],
   "source": [
    "variables = tf.trainable_variables(scope=\"my_model\")\n",
    "print(variables)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在 call 方法中，通过使用 tf.variable_scope 和 reuse 参数来控制变量的重用。这样，在每次调用模型时，变量 self.dense 都会被重用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 多层RNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义 RNN 模型的参数\n",
    "num_layers = 3\n",
    "hidden_size = 128\n",
    "sequence_length=100\n",
    "input_size=10\n",
    "\n",
    "# 创建 RNN 单元列表\n",
    "cells = []\n",
    "for _ in range(num_layers):\n",
    "    cell = tf.nn.rnn_cell.LSTMCell(hidden_size)\n",
    "    cells.append(cell)\n",
    "\n",
    "# 创建多层 RNN 模型\n",
    "multi_cell = tf.nn.rnn_cell.MultiRNNCell(cells)\n",
    "\n",
    "# 定义输入数据\n",
    "inputs = tf.placeholder(tf.float32, [None, sequence_length, input_size])\n",
    "\n",
    "# 使用多层 RNN 模型进行前向传播\n",
    "outputs, final_state = tf.nn.dynamic_rnn(multi_cell, inputs, dtype=tf.float32)\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
